package com.kern.jpademo.service;

import com.kern.jpademo.dao.IWorkerDAO;
import com.kern.jpademo.entity.XWorker;
import com.kern.jpademo.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;

/**
 * Created by caoshi at 20:30 2021-08-03
 */
@Service
public class WorkerService {

    @Autowired
    private IWorkerDAO workerDAO;

    @PersistenceContext
    private EntityManager entityManager;


    public void testSave() {
        XWorker worker = new XWorker();
        worker.setId(CommonUtil.getUUID());
        worker.setName("zs");
        worker.setAge(12);
        worker.setSalary(2000);
        workerDAO.save(worker);
    }


    public void testPersist() {
        //EntityManager entityManager = entityManagerFactory.createEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();

        XWorker worker = new XWorker();
        worker.setId(CommonUtil.getUUID());
        worker.setName("lisi");
        worker.setAge(14);
        worker.setSalary(2100);
        entityManager.persist(worker);

        transaction.commit();
    }





}
